#ifndef FRAME_H
#define FRAME_H
#include <sys/types.h>
#include <string.h>
#include <iostream>
#include "utility.hpp"
#include "typetransfer.h"
#include "predef.h"
class AsynSocketClient;
class Frame
{
public:
    size_t input_index;
//    static const u_char begin1;
//    static const  u_char begin2;
//    static const  u_char version;
    //const u_int16_t msg_id_;
    u_char data[kSocketBufferLen];          //temp_data
    u_char recv_data[kSocketBufferLen];          //temp_data
    Frame()//(const u_int16_t msg_id=3):msg_id_(msg_id)
    {
        input_index=0;
        memset(data,0,kSocketBufferLen);
        memset(recv_data,0,kSocketBufferLen);
        data[0]=SunlinkProtocal::kSunlinkHeader1;
        data[1]=SunlinkProtocal::kSunlinkHeader2;
        data[2]=SunlinkProtocal::kSunlinkVersion;
        data[3]=0;//len_low
        data[4]=0;//len_high
    }

    bool ParseRecvData(const size_t bytes_transferred,boost::shared_ptr<AsynSocketClient> client);


/*
    bool parse_recv_data(const u_char *recv_data,const size_t bytes_transferred,u_char *usr_data,size_t &usr_data_len)
    {
        bool flag=false;

        for (size_t i=0;i<bytes_transferred;i++)
        {
            switch (input_index)
            {
            case 0:
                if(recv_data[i]==Frame::begin1)
                {
                    input_index++;
                }
                break;
            case 1:
                if(recv_data[i]==Frame::begin2)
                {
                    input_index++;
                }
                else
                {
                    if(recv_data[i]==Frame::begin1)
                        input_index=1;
                    else
                        input_index=0;
                }
                break;
            case 2:
                if(recv_data[i]==Frame::version)
                {
                    input_index++;
                }
                else
                {
                    if(recv_data[i]==Frame::begin1)
                        input_index=1;
                    else
                        input_index=0;
                }
                break;
            case 3:case 4:
                data[input_index]=recv_data[i];
                input_index++;
                break;
            default:
                {
                    u_int16_t usr_data_length=data[3]+data[4]*(2^8);
                    if(usr_data_length>input_index-5+bytes_transferred-i)
                    {
                        memcpy(data+input_index,recv_data+i,bytes_transferred-i);
                        input_index=input_index+bytes_transferred-i;
                        i=bytes_transferred;
                    }
                    else {
                        memcpy(data+input_index,recv_data+i,usr_data_length-(input_index-5));
                        i+=usr_data_length-(input_index-4);
                        input_index=0;

                        u_int16_t cal_crc=CalCRC16(data,usr_data_length+3);
                        u_int16_t recv_crc=data[usr_data_length+5-2]+data[usr_data_length+5-1]*256;
                        std::cout<<"cal_crc:"<<cal_crc<<" recv_crc:"<<recv_crc<<std::endl;
                        if(cal_crc==recv_crc)
                        {
                            memcpy(usr_data,data+5,usr_data_length);
                            usr_data_len=usr_data_length;
                            flag=true;

//                            std::cout<<"got all usr data"<<std::endl;
//                            for(size_t k = 5; k < usr_data_length+5; k++)
//                                std::cout << (int)data[k]<<" ";
//                            std::cout << std::endl;
                        }
                        else {

                            std::cout<<"wrong crc, frame dropped"<<std::endl;
                        }
                    }
                }
                break;
            }
        }
        return flag;
    }
*/

};


#endif // FRAME_H
